package com.th.supcom.pds.recipefee.client.widget;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.i18n.client.NumberFormat;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.widget.MedicareCardField;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

public final class RecipeFeeUtils
{
    /**
     * 给查询文本框添加按Enter执行查询事件
     * 
     * @param component
     * @param eventType add by Zli
     */
    private RecipeFeeUtils ()
    {

    }

    public static void enterKeyDownListener (Field <String> component, final EventType eventType)
    {
        component.addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (final ComponentEvent event)
            {
                if (event.getKeyCode () == KeyCodes.KEY_ENTER)
                {
                    final AppEvent event1 = new AppEvent (eventType);
                    Dispatcher.forwardEvent (event1);

                }
            }
        });
    }

    /**
     * 添加按钮监听事件
     * 
     * @param button
     * @param type add by Zli
     */
    public static void buttonListener (final Button button, final EventType type)
    {
        button.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final AppEvent event = new AppEvent (type);
                Dispatcher.forwardEvent (event);
            }
        });
    }

    public static void disableCellEdit (Grid <PdsPrescDetailInfoModel> grid)
    {
        grid.addListener (Events.BeforeEdit, new Listener <GridEvent <ModelData>> ()
        {
            @Override
            public void handleEvent (final GridEvent <ModelData> be)
            {

                final ModelData model = be.getModel ();

                if (disableCondition (be, model))
                {
                    be.setCancelled (true);
                }

            }

        });
    }

    public static boolean disableCondition (final GridEvent <ModelData> be, final ModelData model)
    {
        return 11== be.getColIndex () && model.get ("stockFlag") == null;
    }

    /**
     * 主处方标示颜色
     * 
     * @return add by Zli
     */
    public static GridCellRenderer <ModelData> performbyRender ()
    {
        return new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                if (model.get ("stockFlag") != null)
                {
                    return "<div style='background-color:#8DB6CD;width:100px;height:14px;'>"
                           + ClientUtils.avoidNull (model.get (property)) + "</div>";
                }

                return ClientUtils.avoidNull (model.get (property));
            }
        };
    }

    /**
     * 增加颜色说明面板
     * 
     * @return add by Zli
     */
    public static ToolBar createColorShow ()
    {
        ToolBar toolBar;
        toolBar = new ToolBar ();
        String txt = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                     + "<span class='redTendGrade'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;为已收费处方</spqn> <span class='orangeTendGrade'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;为需要审核处方</span>"
                     + "<span class='greenTendGrade'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;为库存不足处方</span>"
                     + "<span class='blueTendGrade'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;为非本科室处方</span>";
        Label lab = new Label (txt);
        lab.setStyleAttribute ("margin-left", "10px");
        toolBar.add (lab);
        return toolBar;
    }

    /**
     * 初始化患者信息
     * 
     * add by Zli
     */
    public static PatientInfoModel initPatientInfo (MedicareCardField cardField)
    {
        PatientInfoModel model = cardField.getModelValue ();

        return model;
    }

    public static void initPatientData (PatientInfoModel paitentInfo, RecipeFeeFields fields)
    {
        fields.getNameField ().setValue (paitentInfo.getName ());
        fields.getPatientIdField ().setValue (paitentInfo.getPatiId ());
        fields.getSexField ().setValue (paitentInfo.getSex ());
        //DateTimeFormat dtFormat = DateTimeFormat.getFormat ("yyyy-MM-dd");
        fields.getBirthDayField ().setValue (paitentInfo.getAge ());
        // 余额
        if(paitentInfo.getBalance () !=null){
            fields.getBalanceField ().setValue (paitentInfo.getBalance ()+"");
        }
        if (paitentInfo.getChargeType () != null)
        {
            fields.getVisitTypeNameField ().setValue (paitentInfo.getChargeType ());
        }
        if (paitentInfo.getVisitId () != null)
        {
            fields.getVisitIdField ().setValue (paitentInfo.getVisitId () + "");
        }
        /*
         * if (paitentInfo.getUpdateTime () != null) {
         * m_fields.getVisitBeginDateField ().setValue (dtFormat.format
         * (paitentInfo.getUpdateTime ())); }
         */

        fields.getDeptNameField ().setValue (paitentInfo.getDeptName ());
        if(paitentInfo.getVisitDay ()!=null){
            fields.getDay ().setValue (paitentInfo.getVisitDay () + "");
        }
        fields.getNumber ().setValue (paitentInfo.getBedCode ());
    }

    /**
     * 清屏 add by Zli
     */
    public static void clearInfo (RecipeFeeMainPanel mainPanel)
    {
        mainPanel.getFields ().getRecipeNoField ().clear ();
        mainPanel.getFields ().getPrescSource ().clear ();
        mainPanel.getFields ().getNameField ().clear ();
        mainPanel.getFields ().getPatientIdField ().clear ();
        mainPanel.getFields ().getSexField ().clear ();
        mainPanel.getFields ().getBirthDayField ().clear ();
        mainPanel.getFields ().getBalanceField ().clear ();

        mainPanel.getFields ().getVisitIdField ().clear ();
        mainPanel.getFields ().getVisitBeginDateField ().clear ();
        mainPanel.getFields ().getDeptNameField ().clear ();
        mainPanel.getFields ().getDay ().clear ();
        mainPanel.getFields ().getNumber ().clear ();
        mainPanel.getFields ().getVisitTypeNameField ().clear ();
        
        mainPanel.getMedicareCardField ().clear ();
        mainPanel.getRecipePanel ().getGrid ().getStore ().removeAll ();
        mainPanel.getPrescDetailGrid ().clear ();
    }

    public static void formatPosIntEditor (ColumnConfig column)
    {
        final NumberField numberField = new NumberField (); // 正则表达式
        numberField.setRegex ("^[0-9]*[1-9][0-9]*$");
        numberField.setTitle ("请输入正整数");
        numberField.setFormat (NumberFormat.getFormat ("0"));
        column.setEditor (new CellEditor (numberField));
    }

    /**
     * 公司版权信息.
     */
    public static final String copyright = "Copyright 2014, Carefx Corporation";

    /**
     * 提交路径.
     */
    public static final String SCC_BRANCH = "$URL$";

    /**
     * 提交日期.
     */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /**
     * 最终修改人员.
     */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /**
     * 最终版本号.
     */
    public static final String SCC_REVISION = "$Revision$";
}
